/*
 * user.h
 *
 *  Created on: Nov 6, 2024
 *      Author: user
 */

#ifndef _USER_H_
#define _USER_H_

#include "stm32l4xx_hal.h"

#define DEV_NAME_0              "ECNU-CGME"
#define BLE_CMD_SET_NAME        "TTM:REN-"
#define BLE_CMD_SET_BAUDRATE    "TTM:BPS-"
#define DAC_PWM_FREQ	        100
#define USART3_RECVBUFF_SIZE    100
#define HAL_USART_USB           huart1
#define HAL_USART_BLE           huart3

typedef struct
{
	char name[30];			// device name, including two parts: general name and UUID, e.g. REN-ECNUECTD-4500AB

	float volt1;			// output voltage 1
	float volt2;			// output voltage 2
	float volt3;			// output voltage 3
	float volt4;			// output voltage 4
	float volt5;			// output voltage 5
	float volt6;			// output voltage 6
	float volt7;			// output voltage 7
	float volt8;			// output voltage 8
	float volt9;			// output voltage 9
	float volt10;			// output voltage 10
	float volt11;			// output voltage 11
	float volt12;			// output voltage 12
	float volt13;			// output voltage 13
	float volt14;			// output voltage 14
	float volt15;			// output voltage 15
	float volt16;			// output voltage 16

	float rest1;			// sensor resistance 1
	float rest2;			// sensor resistance 2
	float rest3;			// sensor resistance 3
	float rest4;			// sensor resistance 4
	float rest5;			// sensor resistance 5
	float rest6;			// sensor resistance 6
	float rest7;			// sensor resistance 7
	float rest8;			// sensor resistance 8
	float rest9;			// sensor resistance 9
	float rest10;			// sensor resistance 10
	float rest11;			// sensor resistance 11
	float rest12;			// sensor resistance 12
	float rest13;			// sensor resistance 13
	float rest14;			// sensor resistance 14
	float rest15;			// sensor resistance 15
	float rest16;			// sensor resistance 16

	uint16_t batVolt;		// battery voltage
	uint16_t batLife;		// battery life, represented by percentage
	uint16_t temp_x10;		// real temperature x 10
	uint16_t humi_x10;		// real humidity x 10
} DevInfo;

extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_adc1;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;
extern I2C_HandleTypeDef hi2c1;

extern uint8_t usart3_RecvBuff[USART3_RECVBUFF_SIZE];
extern char *JSON_CmdBuff[USART3_RECVBUFF_SIZE];
extern DevInfo device_info;
extern uint8_t is_BLE_configured;

void User_Init();
void User_GenerateDevName(char *name_buffer);
void parseJsonCmd(const char *jsonString, char *parsedCmd);
void parseCmd(const char *cmdString);
uint8_t User_BatVolToPowerPercent(uint16_t bat_vol);
void User_RefreshData();
void User_SendDeviceStatus();
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);


#endif /* USER_H_ */
